const { ActionKey, Action, localFile, GraphNode, loading, modelService, projectService } = require("@/mdesign-lib/mdesign-app.umd.js");
import { FMUOptionWindow } from "./fmuOption";
import { GraphModel, Shape } from "@/mdesign-lib/mdesign-graph.umd.js";

export class ImportFMUAction extends Action {
  keyName = ActionKey.importFMU;

  async run() {
    const project = app.activeProject;
    if (!project) throw new Error("当前无打开项目！");
    const res = await localFile.chooseFile({
      filters: [
        {
          name: "Reqif Files",
          extensions: ["fmu"]
        }
      ]
    });
    if (res.canceled) return;
    const filePath = res.files[0]?.path;
    if (!filePath) return;
    const selectedNodes = app.activeProject.getSelectedNodes();
    let currentId = "";
    if (selectedNodes.length) {
      currentId = selectedNodes[0].data.id;
      const isDisabled = await modelService.isModelDisabled(currentId, project.id);
      if (isDisabled) {
        currentId = "";
      }
    } else {
      const root = app.activeProject.nodePool.rootNodes[0];
      currentId = (!root.disabled && root.data.id) || "";
    }
    if (!currentId) {
      app.$notify({
        type: "error",
        title: "提示",
        message: "对当前包无写权限"
      });
      return;
    } else {
      loading.popup("正在导入FMU文件...");
      const responese = await projectService
        .importFMU({
          projectId: project.id,
          filePath
        })
        .finally(() => loading.close());
      FMUOptionWindow.popup({
        projectId: project.id,
        data: JSON.stringify(responese),
        isEdit: "false",
        parentId: currentId
      });
    }
  }

  onSysMenuClick() {
    const project = app.activeProject;
    if (!project) throw new Error("当前无打开项目！");
    this.run();
  }

  onTreeMenuClick(graphNode: GraphNode) {
    if (!graphNode) return;
    this.run();
  }
}
export class UpdateFMUAction extends Action {
  keyName = ActionKey.updateFMUFile;

  async run(modelId: string) {
    const project = app.activeProject;
    if (!project) throw new Error("当前无打开项目！");
    // 更新fmu 根据当前id
    const node = app.activeProject.nodePool.getNode(modelId);
    if (!node) return;
    const isDisabled = await modelService.isModelDisabled(modelId, project.id);
    if (isDisabled) {
      app.$notify({
        type: "error",
        title: "提示",
        message: "对当前包无写权限"
      });
      return;
    } else {
      const responese = await projectService
        .editFmuOptionData({
          projectId: project.id,
          modelId
        })
        .finally(() => loading.close());
      responese.modelName = node.data.name;
      FMUOptionWindow.popup({
        projectId: project.id,
        data: JSON.stringify(responese),
        isEdit: "true",
        modelId,
        parentId: modelId
      });
    }
  }
  onShapeMenuClick(graph: GraphModel, shape?: Shape) {
    if (shape?.modelId) {
      this.run(shape?.modelId);
    }
  }
  onSysMenuClick() {
    const project = app.activeProject;
    if (!project) throw new Error("当前无打开项目！");
    this.run(project.nodePool.rootNodes[0].id);
  }

  onTreeMenuClick(graphNode: GraphNode) {
    if (!graphNode) return;
    this.run(graphNode.id);
  }
}
